<?php
namespace Seller\Model;
use Think\Model;
class PayModel extends Model 
{
	public $tableName = 'pay';

	//获取所有的地区分类
	public function index($tableName)
	{
		$areaModel = M($tableName);
		$res = $areaModel->select();
		return $res;
	}

	//充值积分
	public function toBuy($arr)
	{
		//需要支付的金额
		$configModer = M('config');
		$configRes   = $configModer->select();
	    $buy = $configRes[0]['buy'];
		
		$num = $arr['num'];
		$buy = $arr['buy'];
		$money = $arr['money'];

		$pay['money'] = $money;
		$pay['shop_id'] = $arr['id'];
		$pay['shijian'] = date('Y-m-d H:i:s',time());

		$random = $this->random();
		$pay['id'] = $random;

		$payModel = M($this->tableName);
		$res_in = $payModel->data($pay)->add();
		
		if($res_in)
		{
			$rechargeModel = M('recharge');
			$recharge['points'] = $money*$buy;
			$recharge['shop_id'] = $arr['id'];
			$recharge['reason'] = '购买积分';
			$recharge['shijian'] = date('Y-m-d H:i:s',time());
			$res = $rechargeModel->data($recharge)->add();
			if($res)
			{
				return 1;
			}
		}
	}

	//提现申请
    public function cash($arr)
    {
        $shopModel = M('shop');

        $shop['shop_id'] = $arr['shop_id'];

        $res = $shopModel->where($shop)->select();

        if($res[0]['use_points'] < 100)
        {
            return 'noNum';
        }else
        {
            $pay['shop_id'] = $arr['shop_id'];
            $pay['money']  = -$arr['number'];
            $pay['apply'] = 0;
            $pay['isApply'] = 0;
            $pay['shijian'] = date('Y-m-d H:i:s',time());

            $random = $this->random();
            $pay['id'] = $random;
            
            $payModel = M($this->tableName);
            $res_in = $payModel->data($pay)->add();

            if($res_in)
            {
            	shopPoints($arr['shop_id']);
                return 'in';
            }else
            {
                return 'no';
            }
        }
    }

	public function random()
	{
		return date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
	}	
}
